package com.xuecheng.content.controller;

import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseBaseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVO;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
//@RequestMapping("courseBase")
public class CourseBaseController implements CourseBaseApi {

    @Autowired
    private CourseBaseService  courseBaseService;

//    @GetMapping("/course/all")
//    public List<CourseBase> queryAll(){
//        return courseBaseService.queryAll();
//    }


    @PostMapping("course/list")
    public PageVO queryCourseBaseList(PageRequestParams params,
                                      @RequestBody QueryCourseBaseModel model) {
        //TODO:获取令牌并解析拿到公司的id值
        Long companyId = SecurityUtil.getCompanyId();
        return courseBaseService.queryCourseBaseList(params,model,companyId);
    }

    @PostMapping("course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        //TODO:本项目规定,课程新增和修改操作,servicce层接收的必须是dto数据

        //1.将VO数据转为dto数据
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2Dto(courseBaseVO);

        //2.获得教学机构的id
        Long companyId = SecurityUtil.getCompanyId();
        courseBaseDTO.setCompanyId(companyId);
        //3.调用sercice层,返回结果
        return courseBaseService.createCourseBase(courseBaseDTO);
    }

    //TODO:还未测试成功
    @GetMapping("/course/{courseBaseId}")
    public CourseBaseDTO getById(@PathVariable Long courseBaseId) {
        Long companyId = SecurityUtil.getCompanyId();
        return courseBaseService.getByCourseId(courseBaseId,companyId);
    }

    @PutMapping("/course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVO courseBaseVO) {
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2Dto(courseBaseVO);
        Long companyId = SecurityUtil.getCompanyId();
        courseBaseDTO.setCompanyId(companyId);
        return courseBaseService.modifyCourseBase(courseBaseDTO);
    }

    @DeleteMapping("/course/{coursebaseId}")
    public void removeCourseBase(@PathVariable("coursebaseId") Long courseBaseId) {
        Long companyId = SecurityUtil.getCompanyId();
        courseBaseService.removeCourseBase(courseBaseId,companyId);
    }

    @GetMapping("/course/commit/{courseBaseId}")
    public void commitCourseBase(@PathVariable Long courseBaseId) {
        Long companyId = SecurityUtil.getCompanyId();
        courseBaseService.commitCourseBase(courseBaseId,companyId);
    }


}
